<?php
    namespace Home\Controller;
    use Think\Controller;
    class ManageController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $list = M("comm.sys_users")->select();
        
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = count($list);
		
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("comm.sys_users")->where("uuid = '$uuid'")->find();
		unset($info['pass_word']);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);
		
		if(empty($data['pass_word']))
		{
			unset($data['pass_word']);
		}
		else
		{
			$data['pass_word'] = md5($data['pass_word']);
		}
		
        $result = M("comm.sys_users")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
		$data['pass_word'] = md5($data['pass_word']);
        $data['uuid'] = $uuid;
        $data["create_date"] = time();
        $result = M("comm.sys_users")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function set_field()
    {
        $data_all = I("post.");

        $accounting_date    = $data_all["accounting_date"];
        $item_code          = $data_all["item_code"];
        $uuid               = $data_all["uuid"];
        $dept_code          = $data_all["dept_code"];
        $cost_item_code     = $data_all["cost_item_code"];
        $field              = $data_all["field"];


        
        $where['accounting_date'] = strtotime($accounting_date);
        $where['dept_code'] = $dept_code;
        $where['cost_item_code'] = $cost_item_code;

        $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

        if(empty($info))
        {
            $where[$data_all['field']] = $data_all['value'];

            $result = M("cbhs.cbhs_dept_cost_detail")->add($where);
            
            $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

            $d['real_costs'] = $info['costs'] + $info['costs_armyfree'];

            M("cbhs.cbhs_dept_cost_detail")->where($where)->save($d);
        }
        else
        {
            $data[$data_all['field']] = $data_all['value'];

            $result = M("cbhs.cbhs_dept_cost_detail")->where($where)->save($data);
            $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

            $d['real_costs'] = $info['costs'] + $info['costs_armyfree'];
            M("cbhs.cbhs_dept_cost_detail")->where($where)->save($d);
        }
        
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
        $this->ajaxReturn($json,"json");
    }

    public function get_dept_list()
    {
        $list = M("comm.sys_dept_dict")->select();
		for($i = 0 ; $i < count($list) ; $i++)
		{
			$list[$i]['checked'] = false;
		}
		
		$uuid = I('post.uuid');
		$user_info = M('comm.sys_users')->where("uuid = '$uuid'")->find();
		$dept_list = $user_info['dept_list'];
		$dept_list = explode(',',$dept_list);
		
		
		
		for($i = 0 ; $i < count($list) ; $i++)
		{
			for($j = 0 ; $j < count($dept_list) ; $j++)
			{
				if($dept_list[$j] == $list[$i]['dept_code'])
				{
					$list[$i]['checked'] = true;
				}
			}
		}
		
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }
	
	public function get_role_list()
	{
	    $list = M("comm.sys_role_list")->select();
	    $json["data"]   = $list;
	    $json["code"]   = 0;
	    $json["msg"]    = "";
	    $this->ajaxReturn($json,"json");
	}
	
	public function get_user_dept_list()
	{
		
	}
	
	public function save_user_dept_list()
	{
		//$uuid = I('post.uuid');
		$data = I('post.');
		$uuid = I('post.uuid');
		$dept = I('post.dept');
		if(!empty($dept['allchoose']))
		{
			unset($dept['allchoose']);
		}
		$dept_list = "";
		foreach($dept as $key=>$value)
		{
			$dept_list .= $key;
			$dept_list .= ",";
		}
		$dept_list = rtrim($dept_list,",");
		$data['dept_list'] = $dept_list;
		$result = M('comm.sys_users')->where("uuid = '$uuid'")->save($data);
		
		if($result !== false)
		{
		    $json["code"] = 0;
		    $json["msg"] = "修改成功";
		}
		else
		{
		    $json["code"] = -1;
		    $json["msg"] = "修改失败";
		}
		$this->ajaxReturn($json,"json");
	}
	
	public function edit_password()
	{
		$admin_uuid = I('post.admin_uuid');
		$old_password = I('post.old_password');
		$new_password = I('new_password');
		$new_password_2 = I('post.new_password_2');
		
		if($new_password != $new_password_2)
		{
			$json['msg'] = "两次密码输入不一致";
			$json['code'] = -1;
			$this->ajaxReturn($json,"json");
		}
		
		$data['uuid'] = $admin_uuid;
		$data['password'] = md5($old_password);
		
		$info = M('comm.sys_users')->where($data)->find();
		unset($data);
		if(!empty($info))
		{
			$data['password'] = md5($new_password);
			$result = M('comm.sys_users')->where("uuid = '$admin_uuid'")->save($data);
			
			if($result !== false)
			{
				$json['msg'] = "修改成功";
				$json['code'] = 0;
			}
			else
			{
				$json['msg'] = "修改失败,请重试";
				$json['code'] = -1;
			}
		}
		else
		{
			$json['msg'] = "原密码错误";
			$json['code'] = -1;
		}
		$this->ajaxReturn($json,"json");
	}

}